package com.ruoyi.web.controller.student_club.admin_controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.student_club.domain.ClubRecommend;
import com.ruoyi.student_club.dto.ClubDto;
import com.ruoyi.student_club.service.IClubRecommendService;
import com.ruoyi.student_club.utils.UploadUtil;
import com.ruoyi.student_club.vo.ClubVo;
import com.ruoyi.student_club.vo.RecommendVo;
import com.ruoyi.web.controller.student_club.domain.ClubParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.student_club.service.IClubService;
import com.ruoyi.common.core.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;


/**
 * 社团列表展示Controller
 *
 * @author 无尤
 * @date 2022-01-14
 */
@Controller
@RequestMapping("/club/club")
public class ClubController extends BaseController {

    private final String prefix = "club/club";

    @Resource
    private IClubService clubService;

    @Resource
    private IClubRecommendService recommendService;

    @GetMapping()
    public String message()
    {
        return prefix + "/club";
    }

    /**
     * 获取社团列表
     * @param clubParam
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult page(ClubParam clubParam){
        Page<ClubVo> page = clubParam.getPageInfo();
        Page<ClubVo> clubVoPage = clubService.queryPage(page,clubParam.getTypeName(),clubParam.getClubName());
        return AjaxResult.success(clubVoPage);
    }

    /**
     * 上传社团头像
     * @param file 文件
     * @return 图片地址
     */
    @ResponseBody
    @PostMapping("/upload")
    public AjaxResult upload(@RequestBody MultipartFile file){
        String typeName = "club_avatar";
        String upload = UploadUtil.upload(file,typeName);
        return AjaxResult.success(upload);
    }

    /**
     * 新增社团
     * @return 新增状态
     */
    @ResponseBody
    @PostMapping("save")
    public AjaxResult save(@RequestBody ClubDto clubDto){
        clubService.save(clubDto);
        return AjaxResult.success();
    }

    /**
     * 社团修改
     * @param clubDto 社团类型
     * @return 修改状态
     */
    @ResponseBody
    @PostMapping("/update")
    public AjaxResult update(@RequestBody ClubDto clubDto){
        clubService.update(clubDto);
        return AjaxResult.success();
    }

    /**
     * 删除社团
     * @param clubId 社团id
     * @return 删除状态
     */
    @ResponseBody
    @DeleteMapping("/{clubId:\\d+}")
    public AjaxResult remove(@PathVariable("clubId") Long clubId){
        clubService.remove(clubId);
        ClubRecommend recommend = recommendService
                .getOne(new QueryWrapper<ClubRecommend>().eq("club_id", clubId));
        if (recommend == null){
            return AjaxResult.success();
        }else {
            recommendService.removeById(recommend.getId());
        }
        return AjaxResult.success();
    }

    /**
     * 获取社团列表
     * @return
     */
    @ResponseBody
    @GetMapping("/getClubList")
    public AjaxResult getClubList(){
        List<RecommendVo> list = clubService.clubList();
        return AjaxResult.success(list);
    }

}
